import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["onFinish", "step", "formRef", "title", "stepProps"];
import { jsx as _jsx } from "react/jsx-runtime";
import { noteOnce } from "rc-util/es/warning";
import { useContext, useEffect, useImperativeHandle, useRef } from 'react';
import { BaseForm } from '../../BaseForm';
import { StepsFormProvide } from './index';

function StepForm(props) {
  var formRef = useRef();
  var context = useContext(StepsFormProvide);

  var _onFinish = props.onFinish,
      step = props.step,
      propFormRef = props.formRef,
      title = props.title,
      stepProps = props.stepProps,
      restProps = _objectWithoutProperties(props, _excluded); // eslint-disable-next-line @typescript-eslint/dot-notation


  noteOnce(!restProps['submitter'], 'StepForm 不包含提交按钮，请在 StepsForm 上');
  /** 重置 formRef */

  useImperativeHandle(propFormRef, function () {
    return formRef.current;
  });
  /** Dom 不存在的时候解除挂载 */

  useEffect(function () {
    if (!(props.name || props.step)) return;
    var name = (props.name || props.step).toString();
    context === null || context === void 0 ? void 0 : context.regForm(name, props);
    return function () {
      context === null || context === void 0 ? void 0 : context.unRegForm(name);
    }; // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  if (context && (context === null || context === void 0 ? void 0 : context.formArrayRef)) {
    context.formArrayRef.current[step || 0] = formRef;
  }

  return _jsx(BaseForm, _objectSpread({
    formRef: formRef,
    onFinish: function () {
      var _onFinish2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(values) {
        var success;
        return _regeneratorRuntime().wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                if (restProps.name) {
                  context === null || context === void 0 ? void 0 : context.onFormFinish(restProps.name, values);
                }

                if (!_onFinish) {
                  _context.next = 9;
                  break;
                }

                context === null || context === void 0 ? void 0 : context.setLoading(true); // 如果报错，直接抛出

                _context.next = 5;
                return _onFinish === null || _onFinish === void 0 ? void 0 : _onFinish(values);

              case 5:
                success = _context.sent;

                if (success) {
                  context === null || context === void 0 ? void 0 : context.next();
                }

                context === null || context === void 0 ? void 0 : context.setLoading(false);
                return _context.abrupt("return");

              case 9:
                if (!(context === null || context === void 0 ? void 0 : context.lastStep)) context === null || context === void 0 ? void 0 : context.next();

              case 10:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }));

      function onFinish(_x) {
        return _onFinish2.apply(this, arguments);
      }

      return onFinish;
    }(),
    layout: "vertical"
  }, restProps));
}

export default StepForm;